/*
	flad : Copyright 2010 Dale J Williams

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

package lads.flad.display {
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.geom.Point;
	
	/**
	 * AlphaBitmap: Combines a diffuse and an alpha bitmap to create a fully
	 * alpha-channeled transparent bitmap.
	 * @author Dale J Williams
	 * @version 1.0
	 */
	public class AlphaBitmap extends Bitmap {
		
		private var _alphaBMD	:BitmapData;
		private var _diffuseBMD	:BitmapData;
		private var _combined	:BitmapData;
		
		public function AlphaBitmap(diffuseBitmapOrData:Object, alphaBitmapOrData:Object) {
			_diffuseBMD = getBitmapFromObject(diffuseBitmapOrData);
			_alphaBMD = getBitmapFromObject(alphaBitmapOrData);
			init();
		}
		
		private function init():void {
			_combined = new BitmapData(_diffuseBMD.width, _diffuseBMD.height);
			_combined.copyPixels(_diffuseBMD, _diffuseBMD.rect, new Point());
			_combined.copyChannel(_alphaBMD, _alphaBMD.rect, new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
			this.bitmapData = _combined;
		}
		
		private function getBitmapFromObject(bitmapOrData:Object):BitmapData {
			if (bitmapOrData is BitmapData) 
				return bitmapOrData as BitmapData;
			if (bitmapOrData is Bitmap)
				return Bitmap(bitmapOrData).bitmapData;
			throw new Error("Invalid Bitmap Data");
		}
		
	}

}